package my.thesis.analytics.metrics;

import org.joda.time.DateTime;

import backtype.storm.metric.api.IMetric;

public class LastTimePersistMetric  implements IMetric
{
	DateTime startTime;
	DateTime timeReceived;
	Boolean hasReceived;
	
	//KALEITAI STHN PREPARE METHODO TWN PERSISTORS META TO INITIALIZATION TWN METRICS
	public void setStartTime()
	{
		this.startTime=DateTime.now();
		this.hasReceived=false;
		this.timeReceived=null;
	}
	
	//KALEITAI STHN EXECUTE METHODO TWN PERSISTORS
	public void setTimeReceived()
	{
		this.hasReceived=true;
		this.timeReceived=DateTime.now();
	}
	
	//METRIETAI H TELEYTAIA STIGMH APO THN ARXH THS TOPOLOGIAS POY ELAVE DEDOMENA TO BOLT ANEKSARTHTA ME TO POTE STELNETAI TO METRIC STO CONSUMER (TIME BUCKET)
	public Object getValueAndReset() 
	{
		String s="";
		if(this.hasReceived)
		{
			long secondsElapsed=(timeReceived.getMillis() - startTime.getMillis())/1000;
	    	s= "Start: " + this.startTime.toString() + "  /  " + "LastReceived: " + timeReceived.toString() +  "  /  " +
	    			 "Seconds Elapsed: " + secondsElapsed;
		}
		else
		{
			s= "Start: " + this.startTime.toString() + "  - NOTHING RECEIVED...BB";
		}
		
		this.hasReceived=false;
		this.timeReceived=null;
		
		// TODO Auto-generated method stub
		return s;
	}

}
